package com.speedchina.common.rsa.service.impl;

import com.speedchina.common.redis.service.RedisService;
import com.speedchina.common.rsa.service.RSAService;
import com.speedchina.common.rsa.util.RSAUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.security.NoSuchAlgorithmException;
import java.util.Map;

/**
 * @author Winter
 */
@Service
public class RSARedisService implements RSAService {
    @Resource
    RedisService redisService;
    @Override
    public String getPrivateKey(String publicKey) {
        return redisService.get(publicKey).toString();
    }

    @Override
    public boolean setPublicKeyPrivateKey(String publicKey, String privateKey) {
        return redisService.set(publicKey,privateKey,60*60);
    }

    @Override
    public boolean removePublicKeyPrivateKey(String publicKey) {
        return redisService.del(publicKey);
    }

    @Override
    public Map<String, String> generatePublicKeyPrivateKey() throws NoSuchAlgorithmException {
        Map<String, String> map = RSAUtils.generatePublicKeyPrivateKey();
        setPublicKeyPrivateKey(map.get("publicKey"),map.get("privateKey"));
        return map;
    }
}
